/**
 * 
 */
package org.dse.proc;

import org.dse.analysis.FullSegmenter;
import org.dse.analysis.Segmenter;
import org.dse.analysis.Token;
import org.dse.beans.HtmlDocument;
import org.dse.html.HtmlParser;
import org.dse.index.DbIndexWriter;
import org.dse.index.IndexWriter;

/**
 * This class provides an interface to process the content of html documents. It
 * gets the plain text(without html tag) and uses a segmenter to form a string
 * of tokens. At last, create index according to the tokens.
 * 
 * @author zhangchen
 * 
 */
public class HtmlContentProcessor {

	private HtmlParser parser;

	private IndexWriter indexWriter;

	/**
	 * The constructor.
	 * 
	 * @param parser
	 *            an HtmlParser object created by the outside. It will be used
	 *            to get the plain text and create the index writer object.
	 * @param document
	 *            an HtmlDocument created by the outside. It indicates the
	 *            current html document processed.
	 */
	public HtmlContentProcessor(HtmlParser parser, HtmlDocument document) {
		this.parser = parser;
		this.indexWriter = new DbIndexWriter(parser, document);
	}

	/**
	 * Set the index writer. Maybe you do not want to use database to store the
	 * indexes, you can implements a new IndexWriter.
	 * 
	 * @param another
	 *            IndexWriter implementation
	 */
	public void setIndexWriter(IndexWriter indexWriter) {
		this.indexWriter = indexWriter;
	}

	/**
	 * The interface to process html content. Segment and index.
	 */
	public void processContent() {
		String contentWithoutTag = parser.getPlainText();

		/*
		 * Create the segmenter to segment the plain text, using FullSegmenter
		 * implementation.
		 */
		Segmenter segmenter = new FullSegmenter(contentWithoutTag);
		Token currentToken = null;
		while ((currentToken = segmenter.next()) != null) {
			indexWriter.addToken(currentToken);
		}

		indexWriter.createIndex();
	}

}
